package com.fw.mediasession.media.mediasession;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.support.v4.media.MediaDescriptionCompat;
import android.support.v4.media.RatingCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.MediaSessionCompat.Callback;
import android.support.v4.media.session.PlaybackStateCompat;

import com.fw.mediasession.media.mediasession.sessionutils.MediaSessionToClientManager;
import com.fw.mediasession.media.player.MediaPlayerFunction;
import com.orhanobut.logger.Logger;

/**
 * 操作mediaPlayer和mediaSession初始化
 */
public class MyMediaSessionManager {
    private String TAG = "MyMediaSessionManager: ";
    private MediaSessionCompat mediaSession;
//    private MediaPlayerUSB mediaPlayerUSB;
    private MediaPlayerFunction mediaPlayerUSB;
    private PlaybackStateCompat mPlaybackState;

    public MyMediaSessionManager(MediaSessionCompat mediaSessionCompat, MediaPlayerFunction mediaPlayerUSBs) {
        this.mediaSession = mediaSessionCompat;
        this.mediaPlayerUSB = mediaPlayerUSBs;
        MediaSessionToClientManager mediaSessionToClientManager=new MediaSessionToClientManager(mediaSessionCompat);
        mediaPlayerUSB.setMediaSessionToClientManager(mediaSessionToClientManager);
        mediaSession.setCallback(callback);

    }

    /**
     * mediaSession控制回调
     */
    Callback callback = new Callback() {
        @Override
        public void onCommand(String command, Bundle extras, ResultReceiver cb) {
            Logger.d("服务端：onCommand command: "+command+" extras: "+extras+" ResultReceiver :"+cb);
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            Logger.d("服务端：onMediaButtonEvent :mediaButtonEvent"+mediaButtonEvent);
            return super.onMediaButtonEvent(mediaButtonEvent);
        }

        @Override
        public void onPrepare() {
            super.onPrepare();
            Logger.d("服务端：onPrepare ");
            mediaPlayerUSB.onPrepare();
        }

        @Override
        public void onPrepareFromMediaId(String mediaId, Bundle extras) {
            super.onPrepareFromMediaId(mediaId, extras);
            Logger.d("服务端：onPrepareFromMediaId mediaId: "+mediaId+" Bundle:"+extras);
        }

        @Override
        public void onPrepareFromSearch(String query, Bundle extras) {
            super.onPrepareFromSearch(query, extras);
            Logger.d("服务端：onPrepareFromSearch query: "+query+" Bundle:"+extras);
            mediaPlayerUSB.onPrepareFromSearch(query,extras);
        }

        @Override
        public void onPrepareFromUri(Uri uri, Bundle extras) {
            super.onPrepareFromUri(uri, extras);
            Logger.d("服务端：onPrepareFromUri Uri: "+uri+" Bundle:"+extras);
            mediaPlayerUSB.onPrepareFromUri(uri,extras);
        }

        @Override
        public void onPlay() {
            Logger.d("服务端：onPlay: ");
            mediaPlayerUSB.onPlay();
        }

        @Override
        public void onPlayFromMediaId(String mediaId, Bundle extras) {
            super.onPlayFromMediaId(mediaId, extras);
            Logger.d("服务端：onPlayFromMediaId mediaId: "+mediaId+" Bundle:"+extras);
            mediaPlayerUSB.onPlayFromMediaId(mediaSession,mediaId,extras);
        }

        @Override
        public void onPlayFromSearch(String query, Bundle extras) {
            super.onPlayFromSearch(query, extras);
            Logger.d("服务端：onPlayFromSearch query: "+query+" Bundle:"+extras);
            mediaPlayerUSB.onPlayFromSearch(query,extras);
        }

        @Override
        public void onPlayFromUri(Uri uri, Bundle extras) {
            super.onPlayFromUri(uri, extras);
            Logger.d("服务端：onPlayFromUri Uri: "+uri+" Bundle:"+extras);
            mediaPlayerUSB.onPlayFromUri(uri,extras);
        }

        @Override
        public void onSkipToQueueItem(long id) {
            super.onSkipToQueueItem(id);
            Logger.d("服务端：onSkipToQueueItem id: "+id);
            mediaPlayerUSB.onSkipToQueueItem(id);
        }

        @Override
        public void onPause() {
            mediaPlayerUSB.onPause();
            Logger.d("服务端：onPause ");
        }

        @Override
        public void onSkipToNext() {
            Logger.d("服务端：onSkipToNext ");
            mediaPlayerUSB.onSkipToNext();
        }

        @Override
        public void onSkipToPrevious() {
            Logger.d("服务端：onSkipToPrevious ");
            mediaPlayerUSB.onSkipToPrevious();
        }

        @Override
        public void onFastForward() {
            Logger.d("服务端：onFastForward ");
            super.onFastForward();
            mediaPlayerUSB.onFastForward();
        }

        @Override
        public void onRewind() {
            Logger.d("服务端：onRewind ");
            super.onRewind();
            mediaPlayerUSB.onRewind();
        }

        @Override
        public void onStop() {
            super.onStop();
            Logger.d("服务端：onStop ");
            mediaPlayerUSB.onStop();
        }

        @Override
        public void onSeekTo(long pos) {
            Logger.d("服务端：onSeekTo pos"+pos);
            mediaPlayerUSB.onSeekTo(pos);
        }

        @Override
        public void onSetRating(RatingCompat rating) {
            Logger.d("服务端：onSetRating RatingCompat"+rating);
            super.onSetRating(rating);
            mediaPlayerUSB.onSetRating(mediaSession,rating);
        }

        @Override
        public void onSetRating(RatingCompat rating, Bundle extras) {
            super.onSetRating(rating, extras);
            Logger.d("服务端：onSetRating RatingCompat"+rating+" Bundle"+extras);
            mediaPlayerUSB.onSetRating(mediaSession,rating,extras);
        }

        @Override
        public void onSetPlaybackSpeed(float speed) {
            super.onSetPlaybackSpeed(speed);
            Logger.d("服务端：onSetPlaybackSpeed speed"+speed);
            mediaPlayerUSB.setPlaySpeed(speed);
        }

        @Override
        public void onSetCaptioningEnabled(boolean enabled) {
            super.onSetCaptioningEnabled(enabled);
            Logger.d("服务端：onSetCaptioningEnabled enabled"+enabled);
        }

        @Override
        public void onSetRepeatMode(int repeatMode) {
            super.onSetRepeatMode(repeatMode);
            mediaPlayerUSB.onSetRepeatMode(mediaSession, repeatMode);
            Logger.d("服务端：onSetRepeatMode repeatMode"+repeatMode);
        }

        @Override
        public void onSetShuffleMode(int shuffleMode) {
            super.onSetShuffleMode(shuffleMode);
            Logger.d("服务端：onSetShuffleMode shuffleMode"+shuffleMode);
        }

        @Override
        public void onCustomAction(String action, Bundle extras) {
            super.onCustomAction(action, extras);
            Logger.d("服务端：onCustomAction action: "+action+" Bundle:"+extras);
            mediaPlayerUSB.onCustomAction(mediaSession,action,extras);
        }

        @Override
        public void onAddQueueItem(MediaDescriptionCompat description) {
            super.onAddQueueItem(description);
            Logger.d("服务端：onAddQueueItem MediaDescriptionCompat: "+description);
        }

        @Override
        public void onAddQueueItem(MediaDescriptionCompat description, int index) {
            super.onAddQueueItem(description, index);
            Logger.d("服务端：onAddQueueItem MediaDescriptionCompat: "+description+" index "+index);
        }

        @Override
        public void onRemoveQueueItem(MediaDescriptionCompat description) {
            super.onRemoveQueueItem(description);
            Logger.d("服务端：onRemoveQueueItem MediaDescriptionCompat: "+description);
        }
    };
}
